﻿using System;
using System.Collections.Generic;
using System.Text;
using Tao.Sdl;

namespace Ommf
{
    public class OmmfEngine
    {
        private Timer timer;
        private GraphicsManager graphics;
        private AudioManager audio;
        private KeyboardManager keyboard;
        private MouseManager mouse;

        private bool isOpened = false;
        private Sdl.SDL_Event sdlEvent;

        public OmmfEngine()
        {
            Sdl.SDL_Init(Sdl.SDL_INIT_AUDIO | Sdl.SDL_INIT_VIDEO);

            timer = new Timer();
            graphics = new GraphicsManager(this);
            audio = new AudioManager(this);
            keyboard = new KeyboardManager(this);
            mouse = new MouseManager(this);
        }

        public void Close()
        {
            audio.Close();
            graphics.Close();
            
            Sdl.SDL_Quit();
            
            isOpened = false;
        }

        internal void PollEvents()
        {
            while (Sdl.SDL_PollEvent(out sdlEvent) != 0)
            {
                switch (sdlEvent.type)
                {
                    case Sdl.SDL_QUIT:
                        isOpened = false;
                        break;

                    case Sdl.SDL_KEYDOWN:
                        keyboard.OnKeyPressed((Key)sdlEvent.key.keysym.sym);
                        break;

                    case Sdl.SDL_KEYUP:
                        keyboard.OnKeyReleased((Key)sdlEvent.key.keysym.sym);
                        break;

                    case Sdl.SDL_MOUSEBUTTONDOWN:
                        if (sdlEvent.button.button == Sdl.SDL_BUTTON_LEFT)
                            mouse.OnButtonPressed(Ommf.Mouse.Left);
                        else if (sdlEvent.button.button == Sdl.SDL_BUTTON_MIDDLE)
                            mouse.OnButtonPressed(Ommf.Mouse.Middle);
                        else if (sdlEvent.button.button == Sdl.SDL_BUTTON_RIGHT)
                            mouse.OnButtonPressed(Ommf.Mouse.Right);
                        else if (sdlEvent.button.button == Sdl.SDL_BUTTON_WHEELUP)
                            mouse.OnButtonPressed(Ommf.Mouse.WheelUp);
                        else if (sdlEvent.button.button == Sdl.SDL_BUTTON_WHEELDOWN)
                            mouse.OnButtonPressed(Ommf.Mouse.WheelDown);
                        break;

                    case Sdl.SDL_MOUSEBUTTONUP:
                        if (sdlEvent.button.button == Sdl.SDL_BUTTON_LEFT)
                            mouse.OnButtonReleased(Ommf.Mouse.Left);
                        else if (sdlEvent.button.button == Sdl.SDL_BUTTON_MIDDLE)
                            mouse.OnButtonReleased(Ommf.Mouse.Middle);
                        else if (sdlEvent.button.button == Sdl.SDL_BUTTON_RIGHT)
                            mouse.OnButtonReleased(Ommf.Mouse.Right);
                        else if (sdlEvent.button.button == Sdl.SDL_BUTTON_WHEELUP)
                            mouse.OnButtonReleased(Ommf.Mouse.WheelUp);
                        else if (sdlEvent.button.button == Sdl.SDL_BUTTON_WHEELDOWN)
                            mouse.OnButtonReleased(Ommf.Mouse.WheelDown);
                        break;
                }
            }

            keyboard.Update();
            mouse.Update();
        }

        public Timer Timer
        {
            get { return timer; }
        }

        public GraphicsManager Graphics
        {
            get { return graphics; }
        }

        public AudioManager Audio
        {
            get { return audio; }
        }

        public KeyboardManager Keyboard
        {
            get { return keyboard; }
        }

        public MouseManager Mouse
        {
            get { return mouse; }
        }

        public bool IsOpened
        {
            get { return isOpened; }
            internal set { isOpened = value; }
        }
    }
}
